import homeApi from '@/api/homeApi'
import { showNotify } from 'vant'
import diocmApi from '@/api/diocmApi'
import { AxiosResponse } from 'axios'
import { Ref, ref } from 'vue'
import goodStorage from 'good-storage'
import router from '@/router'

export interface Exam {
  age: string
  describes: string
  diagnosis: string
  fromDeptName: string
  fromDocName: string
  id: string
  inPatientNo: string
  isFinished: string
  machineName: string
  machineType: string
  moduleName: string
  name: string
  oldDocID: string
  outPatientNo: string
  reportDate: string
  reportTime: string
  sex: string
  sickBedNo: string
  visceras: string
}

interface FindExamList {
  rows: Exam[]
}

interface Form {
  StartExamDate: string
  EndExamDate: string
  Name: string
  Sex: string
  StartAge: string
  EndAge: string
  OutPatientNo: string
  InPatientNo: string
  OldDocID: string
  SickBedNo: string
  ModuleName: string
  FromDeptName: string
  FromDocName: string
}

export interface Dicom {
  modality: string
  numImages: string
  patientId: string
  patientName: string
  seriesList: any
  studyDate: string
  studyDescription: string
  studyID: string
}

export type PartialForm = Partial<Form>

class HomeService {
  static homeService = new HomeService()
  examList: Ref<Exam[] | null> = ref(null)
  dicomInfo: Ref<Dicom | null> = ref(null)
  static page: number = 1
  static size: number = 10
  static form: PartialForm
  isFinished: Ref<boolean> = ref(false)
  isLoading: Ref<boolean> = ref(false)

  async findExamList(data: PartialForm) {
    HomeService.reset()
    const { page, size } = HomeService
    HomeService.form = data
    const res: AxiosResponse<FindExamList> = await homeApi.findExamList(
      page,
      size,
      data
    )

    if (res.data.rows.length) {
      HomeService.homeService.isLoading.value = false
    }
    HomeService.homeService.examList.value = res.data.rows
  }

  async handleOnloadData() {
    if (HomeService.homeService.isFinished.value) {
      return
    }
    if (HomeService.homeService.examList.value?.length) {
      HomeService.homeService.isLoading.value = true
      HomeService.page += 1
      const res: AxiosResponse<FindExamList> = await homeApi.findExamList(
        HomeService.page,
        HomeService.size,
        HomeService.form
      )
      HomeService.homeService.isLoading.value = false
      if (res.data.rows.length === 0) {
        HomeService.homeService.isFinished.value = true
        return
      }
      HomeService.homeService.examList.value?.push(...res.data.rows)
    }
  }

  // async findDiocmView(id: string, moduleName: string) {
  //   const res: AxiosResponse<Dicom> = await diocmApi.findDiocmView(
  //     id,
  //     moduleName
  //   )
  //   const data = res.data
  //   goodStorage.set('dicom', data)
  //   if ((HomeService.homeService.dicomInfo.value = res.data)) {
  //     router.push({ path: '/dicom' })
  //   } else {
  //     showNotify({ type: 'warning', message: '该病人没有影像' })
  //   }
  // }

  static reset() {
    HomeService.homeService.examList.value = []
    HomeService.page = 1
    HomeService.size = 10
    HomeService.homeService.isFinished.value = false
    HomeService.homeService.isLoading.value = false
  }
}

export default HomeService.homeService
